<template>
    <div style="text-align: left;">
      <router-link to="/home"><el-i class="fas fa-home" style="color: black;"></el-i></router-link> 
    </div>
  <div class="search-page">
    <h1 class="page-title">模糊搜索</h1>
    <div class="search-box">
      <input
        v-model="searchQuery"
        type="text"
        placeholder="请输入搜索关键词"
        @keyup.enter="performSearch"
      />
      <button @click="performSearch">搜索</button>
    </div>

    <!-- 显示搜索结果 -->
    <div v-if="searchResults.length" class="results">
      <h2>搜索结果</h2>
      <ul>
        <li v-for="result in searchResults" :key="result.PostID" @click="goToBokeInfo(result.PostID)">
          <h3>{{ result.PostTitle }}</h3>
          <p>{{ truncateContent(result.PostContent, 40) }}</p>
          <p>标签: {{ result.TagNames }}</p>
          <p>分类: {{ result.ColumnNames }}</p>
        </li>
      </ul>
    </div>

    <!-- 如果没有搜索结果 -->
    <div v-else-if="isSearchPerformed" class="no-results">
      没有找到相关结果。
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import { useRouter } from 'vue-router'; // 引入 useRouter
import instance from '../api/server'; // 引入 axios 实例

export default {
  setup() {
    const searchQuery = ref(""); // 搜索关键词
    const searchResults = ref([]); // 搜索结果
    const isSearchPerformed = ref(false); // 是否执行过搜索
    const router = useRouter(); // 使用 useRouter

    // 执行搜索
    const performSearch = async () => {
      if (!searchQuery.value.trim()) {
        alert("请输入搜索关键词");
        return;
      }

      try {
        // 发送 POST 请求到后端 API
        const response = await instance.post("/myinfo/blog/selectblog", {
          keyword: `%${searchQuery.value}%`, 
        });

        // 根据后端返回的状态码处理结果
        if (response.data.status === 0) {
          searchResults.value = response.data.data; // 赋值后端返回的搜索结果
        } else {
          console.error("搜索失败:", response.data.message);
          alert(response.data.message || "搜索失败，请稍后重试");
        }
      } catch (error) {
        console.error("搜索失败:", error);
        alert("搜索失败，请稍后重试");
      }

      isSearchPerformed.value = true;
    };

    // 跳转到 BokeInfo 页面
    const goToBokeInfo = (postId) => {
      router.push(`/bokeinfo/${postId}`);
    };

    // 截断内容并添加省略号
    const truncateContent = (content, maxLength) => {
      if (content.length > maxLength) {
        return content.substring(0, maxLength) + '.......';
      }
      return content;
    };

    return {
      searchQuery,
      searchResults,
      isSearchPerformed,
      performSearch,
      goToBokeInfo,
      truncateContent,
    };
  },
};
</script>

<style scoped>
.search-page {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  background-color: #f5f5f5;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  margin-top: 20px;
}

.page-title {
  color: black;
}

.search-box {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

input {
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-right: 10px;
  width: 70%;
  background-color: #fff;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}

button {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #369f6e;
}

.results {
  margin-top: 20px;
  text-align: left;
  background-color: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  padding: 15px;
  border-bottom: 1px solid #eee;
  transition: background-color 0.3s ease;
  cursor: pointer;
}

li:hover {
  background-color: #f9f9f9;
}

h3 {
  margin: 0 0 10px;
  font-size: 18px;
  color: #333;
}

p {
  margin: 5px 0;
  color: #555;
}

.no-results {
  margin-top: 20px;
  color: #888;
  background-color: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style>